import {CRS} from "./CRS";
import {LatLngBounds} from "../geom/LatLngBounds";
import {LatLng} from "../geom/LatLng";
import {Vector2, Vector3} from "three";

export class CRS3857 extends CRS {
    bbox: LatLngBounds;

    projection(latLng: LatLng): Vector2 {
        let x = latLng.longitude * 20037508.342789 / 180;

        let y = Math.log(Math.tan((90 + latLng.latitude) * Math.PI / 360)) / (Math.PI / 180);

        y = y * 20037508.34789 / 180;

        return new Vector2(x, y);
    }

    unProjection(vec: Vector2): LatLng {
        let x = vec.x / 20037508.34 * 180;

        let y = vec.y / 20037508.34 * 180;

        y = 180 / Math.PI * (2 * Math.atan(Math.exp(y * Math.PI / 180)) - Math.PI / 2);

        return new LatLng(y, x);
    }

    constructor() {
        super();

        this.bbox = new LatLngBounds(new LatLng(-85, -180),
            new LatLng(85, 180))
    }
}
